<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>弹性小球效果Taih</title>
<style type="text/css"> 
	div{ padding:0; margin:0; position:absolute;}
	h3{ width:300px; height:40px; line-height:40px; font-size:14px; color:#950B0E; font-family:"微软雅黑"; margin:0 auto;}
	#img1{ width:82px;}
	div img{ width:100px;}
	#div1{ top:0; left:30px;}
	#div2{ top:0; left:130px;}
	#div3{ top:0; left:230px;}
	#div4{ top:0; left:330px;}
	#div5{ top:0; left:430px;}
	#div6{ top:130px; left:30px;}
	#div7{ top:130px; left:130px;}
	#div8{ top:130px; left:330px;}
	#div9{ top:130px; left:230px;}
</style>
<script type="text/javascript"> 
	window.onload=function()
	{
		var oDiv1=document.getElementById('div1');
		var oDiv2=document.getElementById('div2');
		var oDiv3=document.getElementById('div3');
		var oDiv4=document.getElementById('div4');
		var oDiv5=document.getElementById('div5');
		var oDiv6=document.getElementById('div6');
		var oDiv7=document.getElementById('div7');
		var oDiv8=document.getElementById('div8');
		var oDiv9=document.getElementById('div9');
		oDiv1.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv2.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv3.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv4.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv5.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv6.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv7.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv8.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
		oDiv9.onmousedown=function (ev){oEvent=ev||event; drag(this,oEvent);};
	};
	var timer=null;
	function drag(obj,ev)
	{
		var disX=0;
		var disY=0;
		var iSpeedX=0;
		var iSpeedY=0;
		var arr=[];
		(function fnDown()
		{
			disX=ev.clientX-obj.offsetLeft;
			disY=ev.clientY-obj.offsetTop;
			if(document.all)
			{
			}else{
				ev.preventDefault();
			}
			document.onmousemove=function(ev)
			{
				var oEvent=ev||event;
				var l=oEvent.clientX-disX;
				var t=oEvent.clientY-disY;
				if(l>document.documentElement.clientWidth-obj.offsetWidth)
				{
					l=document.documentElement.clientWidth-obj.offsetWidth;
				}
				else if(l<0)
				{
					l=0;
				}
				obj.style.left=l+"px";
				obj.style.top=t+"px";
				arr.push([obj.offsetLeft,obj.offsetTop]);
 
				return false;
			}
			document.onmouseup=function()
			{
				document.onmousemove = null;
				document.onmouseup = null;
				if(arr[0] == null || arr[1] == null)
				{
					iSpeedY=10;
					iSpeedX=10;
				}
				else
				{
					iSpeedY=arr[arr.length-1][1]-arr[arr.length-2][1];
					iSpeedX=arr[arr.length-1][0]-arr[arr.length-2][0];
				}
				clearInterval(obj.timer);
				stratMove();
			}
			clearInterval(obj.timer);
			return false;
		})();
		function stratMove()
		{
			obj.timer=setInterval(doMove,30);
		}
		function doMove()
		{
			iSpeedY+=3;
			if(Math.abs(iSpeedX)<1)
			{
				iSpeedX=0;
				clearInterval(obj.timer);
			}
			var l=obj.offsetLeft+iSpeedX;
			var t=obj.offsetTop+iSpeedY;
			if(l>document.documentElement.clientWidth-obj.offsetWidth)
			{
				iSpeedX *= -1;
				l=document.documentElement.clientWidth-obj.offsetWidth;
			}
			else if(l<0)
			{
				iSpeedX *= -1;
				l=0;
			}
			if(t>document.documentElement.clientHeight-obj.offsetHeight)
			{
				iSpeedY *= -1;
				iSpeedY *=0.75;
				iSpeedX *=0.75;
				t=document.documentElement.clientHeight-obj.offsetHeight;
			}
			obj.style.left=l+"px";
			obj.style.top=t+"px";
		}
	}
</script>
</head>
 
<body>
	<h3>弹性小&#65533;-随便拖个浏览器图&#65533; 试试  O(∩_&#65533;)O~</h3>
	<div style="width:400px; position:absolute; top:20px; right:0; font-size:24px; color:red; font-family:'微软雅黑';">web前端开发交流群&#65533;166284209</div>
	<div id="div1"><img src="http://www.codefans.net/jscss/demoimg/201111/1.png" /></div>
	<div id="div2"><img src="http://www.codefans.net/jscss/demoimg/201111/2.png" /></div>
	<div id="div3"><img src="http://www.codefans.net/jscss/demoimg/201111/3.png" /></div>
	<div id="div4"><img src="http://www.codefans.net/jscss/demoimg/201111/4.png" /></div>
	<div id="div5"><img src="http://www.codefans.net/jscss/demoimg/201111/5.png" /></div>
	<div id="div6"><img src="http://www.codefans.net/jscss/demoimg/201111/6.png" /></div>
	<div id="div7"><img src="http://www.codefans.net/jscss/demoimg/201111/7.png" /></div>
	<div id="div8"><img src="http://www.codefans.net/jscss/demoimg/201111/8.png" /></div>
	<div id="div9"><img src="http://www.codefans.net/jscss/demoimg/201111/9.png" /></div>
</body>
</html>